Raise a descriptive ArgumentError from <=> for incompatible units#32
Merged
Merged
Conversation
b3c7c8e to
4741402
Compare
Following minad#31, <=> returned nil for non-coercible objects, but the Numeric branch still raised IncompatibleUnitError (a TypeError) whenever two units had incompatible dimensions. Routed through Comparable that surfaced as the unhelpful "comparison of Unit with Unit failed", and sort, min, max, between? and clamp blew up with a non-standard error. Raise ArgumentError directly from <=> when the operands are dimensionally incompatible. Every Comparable path funnels through <=>, so the same descriptive message now surfaces from <, >, sort, min, max, between? and clamp. A sibling Unit is shown via #inspect (Unit("1 s")); any other numeric is shown by class (Float, Integer) rather than its coerced unit or a potentially large #inspect, matching core Ruby's own phrasing. Comparing against a genuinely foreign object (nil, String) still returns nil, matching 1 <=> "a"; only dimension mismatches between quantities raise. Arithmetic (+/-) keeps raising IncompatibleUnitError.
4741402 to
16c2bcb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Following up on #31:
<=>now returnsnilfor non-coercible objects, but theNumericbranch still raisedIncompatibleUnitError(aTypeError) whenever two units had incompatible dimensions. Routed throughComparablethat surfaced as the unhelpfulcomparison of Unit with Unit failed, andsort,min,max,between?andclampblew up with a non-standard error.Raise
ArgumentErrordirectly from<=>when the operands are dimensionally incompatible. EveryComparablepath funnels through<=>, so one descriptive message now surfaces consistently from<,>,sort,min,max,between?andclamp.Operands are labelled following core Ruby's own phrasing, symmetrically in both directions: a dimensional
Unitis shown via#inspect, while a bare numeric — or a dimensionless unit, which is just a number and is how a coerced bare numeric arrives — is shown by its class. This avoids splatting an arbitrary#inspectinto the message or printing a confusing coercedUnit("1.2").Comparing against a genuinely foreign object (
nil, aString) still returnsnilfrom<=>, matching1 <=> "a"— only dimension mismatches between quantities raise. Arithmetic (+/-) deliberately keeps raisingIncompatibleUnitError.